package com.bytecollege.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytecollege.mapper.ArticleCollectMapper;
import com.bytecollege.model.ArticleCollect;
import com.bytecollege.service.ArticleCollectService;
import com.bytecollege.vo.ArticleCollectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: CollectServiceImpl
 * @Description:
 * @Author: Sun
 * @Date: 2022/10/18 21:03
 */
@Service
public class ArticleCollectServiceImpl extends ServiceImpl<ArticleCollectMapper, ArticleCollect> implements ArticleCollectService {

    @Autowired
    private ArticleCollectMapper articleCollectMapper;

    /**
     * 获取所有文章收藏
     * @param page
     * @param userId
     * @return
     */
    public List<ArticleCollectVo> getArticleCollect(Page<ArticleCollectVo> page , Integer userId){
        return articleCollectMapper.getArticleCollect(page,userId);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delArticeCollectById(Integer id){
        return articleCollectMapper.del(id);
    }

    public int addCollect(ArticleCollect articeCollect){
        return articleCollectMapper.addCollect(articeCollect);
    }

    /**
     * 查询某帖子的收藏量
     * @param articleId
     * @return
     */
    public Integer getCollectNumByArticleId(Integer articleId){
        return articleCollectMapper.getCollectNumByArticleId(articleId);
    }

    /**
     * 获取文章的收藏状态
     * @param articleId
     * @param userId
     * @return 1：未收藏
     *         0：已收藏
     */
    public Integer getArticleCollectStatus(Integer articleId,Integer userId){
        ArticleCollect collect = articleCollectMapper.getOneArticle(new QueryWrapper<ArticleCollect>().eq("article_id", articleId).eq("user_id", userId));
        if (collect == null){
            return 1;
        }
        return 0;
    }
}